package xyz.labmem.lipx.core.tool

import java.text.DecimalFormat

/**
 * @description: do something
 * @author: ltty
 * @date: 2022/6/17 14:08
 */
class StreamTool {
    companion object {
        fun streamSize(bl: Long): String {
            val KB: Long = 1024 //定义KB的计算常量
            val MB = KB * 1024 //定义MB的计算常量
            val GB = MB * 1024 //定义GB的计算常量
            val TB = GB * 1024 //定义GB的计算常量
            val df = DecimalFormat("0.00") //格式化小数
            val resultSize: String = if (bl / TB >= 1) {
                df.format((bl / TB.toFloat()).toDouble()) + " TB/s"
            } else if (bl / GB >= 1) {
                //如果当前Byte的值大于等于1GB
                df.format((bl / GB.toFloat()).toDouble()) + " GB/s"
            } else if (bl / MB >= 1) {
                //如果当前Byte的值大于等于1MB
                df.format((bl / MB.toFloat()).toDouble()) + " MB/s"
            } else if (bl / KB >= 1) {
                //如果当前Byte的值大于等于1KB
                df.format((bl / KB.toFloat()).toDouble()) + " KB/s"
            } else {
                "$bl B/s"
            }
            return resultSize
        }
    }
}